package www.hz.domain.strategy.service.rule.filter.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import www.hz.domain.strategy.model.entity.RuleActionEntity;
import www.hz.domain.strategy.model.entity.RuleMatterEntity;
import www.hz.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import www.hz.domain.strategy.repository.IStrategyRepository;
import www.hz.domain.strategy.service.annotation.LogicStrategy;
import www.hz.domain.strategy.service.rule.filter.ILogicFilter;
import www.hz.domain.strategy.service.rule.filter.factory.DefaultLogicFactory;

import javax.annotation.Resource;

/**
 * @className: RuleLockLogicFilter
 * @Description: 抽奖n次后，对应奖品可解锁抽奖
 * @version: v1.８.0
 * @author: hz
 * @date: 2025/3/14 10:56
 */
@Component
@Slf4j
@LogicStrategy(logicMode = DefaultLogicFactory.LogicModel.RULE_LOCK)
public class RuleLockLogicFilter implements ILogicFilter<RuleActionEntity.RaffleCenterEntity> {
    @Resource
    private IStrategyRepository repository;
    // TODO: 2025/3/14 用户抽奖次数，后续完成这部分开发流程的时候，从数据库/redis中读取 
    private Long userRaffleCount=0L;
    @Override
    public RuleActionEntity<RuleActionEntity.RaffleCenterEntity> filter(RuleMatterEntity ruleMatterEntity) {
       //查询规则值配置；当前奖品ID，抽奖中规则对应的校验值。如1，2，6
        String ruleValue=repository.queryRuleValue(ruleMatterEntity.getStrategyId(),ruleMatterEntity.getAwardId(),ruleMatterEntity.getRuleModel());
        Long raffleCount=Long.parseLong(ruleValue);
        //用户抽奖次数大于等于规则限定值值则放行
        if(userRaffleCount>=raffleCount){
            return RuleActionEntity.<RuleActionEntity.RaffleCenterEntity>builder()
                    .code(RuleLogicCheckTypeVO.ALLOW.getCode())
                    .info(RuleLogicCheckTypeVO.ALLOW.getInfo())
                    .build();
                    
        }
            //用户抽奖次数小于规则限定值则拦截
            return RuleActionEntity.<RuleActionEntity.RaffleCenterEntity>builder()
                    .code(RuleLogicCheckTypeVO.TAKE_OVER.getCode())
                    .info(RuleLogicCheckTypeVO.TAKE_OVER.getInfo())
                    .build();
    }

}